package month12.demo1208.homework;
import java.util.Arrays;
import java.util.Random;
/*
5.随机产生30个10~20之间的数字，放入数组中，求：
A.输出最大值，最小值。
B.按照从小到大的顺序打印输出。
C.求出现次数最多的数字。
 */
public class Demo_03 {
    public static void main(String[] args) {
        int [] arr = new int[30];
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt(11)+10;
        }
        int max = arr[0];
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (max<arr[i]){
                max = arr[i];
            }
            if (min>arr[i]){
                min = arr[i];
            }
        }
        System.out.println("最大值:"+max);
        System.out.println("最小值:"+min);

        //冒泡排序:
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
        /*
        [10, 10, 10, 10, 10, 10, 11, 11, 12, 12,
        13, 13, 13, 13, 14, 15, 15, 15,
        16, 17, 17, 17, 18, 18, 19, 19, 19, 20, 20, 20]
         */
//        找出数组中出现的次数最多的数字:[10,20]
//        定义一个数组,长度为11,记录每一个数字出现的次数
        int [] counts = new int[11];//[0,10]
    /* for (int i = 0; i < counts.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (i==arr[j]-10){
                    counts[i]++;
                }
            }
        }*/
        for (int i = 0; i < arr.length; i++) {
            counts[arr[i]-10]++;
        }
        System.out.println(Arrays.toString(counts));


    }
}
